package Sort.sort2020_2;

import util.SwapUtil;

/**
 * 冒泡排序
 * 从小到大
 * @author: leiwenkang
 * @date: 2020/5/23
 * @time: 18:17
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] array = {3,8,4,6,7,2,5,9,1};
        sort(array);
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }

    public static void sort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }

        boolean flag = true;
        for (int i = 0; i < array.length - 1 && flag; i++) {
            flag = false;
            for (int j = array.length - 1; j > i; j--) {
                if (array[j] < array[j - 1]) {
                    //交换
                    SwapUtil.swap(array, j, j - 1);
                    flag = true;
                }

            }
        }
    }
}
